package controller

import (
	"gin/common/lib"
	"gin/middleware"
	"gin/model/body"
	"gin/service/userService"
	"gin/util/map_builder"
	"gin/util/request"

	"github.com/gin-gonic/gin"
)

type UserAuthController struct {
}

func UserAuthRegister(router lib.RegisterRouterGroup, needLoginRouter lib.RegisterRouterGroup) {
	userAuth := UserAuthController{}
	router.POST("/login/userLogin", userAuth.login)
	router.POST("/login/register", userAuth.register)
}

// @Summary 登录
// @Description 登录
// @Tags 用户相关
// @Accept json
// @Router /login/userLogin [post]
func (*UserAuthController) login(c *gin.Context) {
	var p body.UserLoginParam
	request.Bind(c, &p)

	userInfo := userService.CheckUserLogin(p)

	token := userService.GetToken(userInfo.Id, p)

	middleware.Success(c, map_builder.BuilderMap("token", token, "id", userInfo.Id, "isFirstMobile", 1))

}

// @Summary 注册
// @Description 注册
// @Tags 用户相关
// @Accept json
// @Router /login/register [post]
func (*UserAuthController) register(c *gin.Context) {
	var p body.UserLoginParam
	request.Bind(c, &p)
	p.Ip = c.ClientIP()

	email, mobile := userService.VerifyUser(p)

	password := userService.VerifyPassword(p.Password)

	uid := userService.AddUser(p.UserLogin, password, p.Source, email, mobile, p.Ip, p.HardwareId, p.Avatar)

	token := userService.GetToken(uid, p)

	middleware.Success(c, map_builder.BuilderMap("token", token, "id", uid, "isFirstMobile", 1, "userName", p.UserLogin))
}
